﻿Imports MyDLL

Public Class Form13

    Dim SaveOpen As New MyDLL.SaveOpen
    Dim Document As New MyDLL.Document

    Private Sub Form13_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ActionLabel.Text = "Program Loaded"

        ComboBox1.SelectedItem = Form1.ComboBox1.SelectedItem
        TextBox2.Text = Form1.TextBox2.Text
        TextBox3.Text = Form1.TextBox3.Text
        TextBox1.Text = Form1.TextBox1.Text
    End Sub

    Sub ViewTable()
        If ComboBox1.SelectedItem = "Drive" Then
            TextBox7.Text = TextBox1.Text & ":\" & TextBox2.Text & "\"
        End If
        If ComboBox1.SelectedItem = "Desktop" Then
            TextBox7.Text = My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & TextBox2.Text & "\"
        End If
        If ComboBox1.SelectedItem = "My Documents" Then
            TextBox7.Text = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & TextBox2.Text & "\"
        End If

        Try
            Dim File As XElement = XElement.Load(TextBox7.Text & TextBox3.Text & ".t.xml")


            Dim choice As XElement = _
            <Node>
                <%= From el In File.<Type> _
                    Select el %>
            </Node>

            If choice = "Email Contacts" Then
                Form14.Type = "Contact"
            ElseIf choice = "Passwords" Then
                Form14.Type = "Service"
            ElseIf choice = "Accounting" Then
                Form14.Type = "Transaction"
            ElseIf choice = "Stock Taking" Then
                Form14.Type = "Transaction"
            ElseIf choice = "Search" Then
                Form14.Type = "result"
            Else
                MsgBox("This file type is not yet supported by this function!!")
            End If
        Catch ex As Exception
            MsgBox("Loading of Document Failed!")
        End Try

        Form14.Show()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ViewTable()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Save()
    End Sub

    Sub Save()

        If CheckBox7.Checked Then
            SaveOpen.FormAdd = False
        Else
            SaveOpen.FormAdd = True
        End If

        ActionLabel.Text = "Saving File"

        SaveOpen.SaveXml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, "Desktop", _
                         "My Documents", TextBox3.Text & ".t", RichTextBox1)

        Me.Text = "Table XML Maker/Reader" & " - " & TextBox3.Text

        If Form1.CheckBox8.Checked Then
            Try
                Dim Data As XDocument = XDocument.Load(TextBox7.Text & TextBox3.Text & ".t.xml")

                My.Computer.FileSystem.WriteAllText(TextBox7.Text & TextBox3.Text & ".t.xml", Data.ToString, False)

            Catch ex As Exception
                MsgBox("Document Formatting Failed!", , "Document Formatting")
            End Try

        End If

        ActionLabel.Text = "Ready"

    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Open()
    End Sub

    Sub Open()

        If CheckBox7.Checked Then
            SaveOpen.FormAdd = False
        Else
            SaveOpen.FormAdd = True
        End If

        ActionLabel.Text = "Opening File"

        SaveOpen.OpenXml(ComboBox1, "Drive", TextBox7, TextBox1.Text, TextBox2.Text, _
                         "Desktop", "My Documents", TextBox3.Text & ".t", RichTextBox1)

        Me.Text = "Table XML Maker/Reader" & " - " & TextBox3.Text

        FormatDoc()

        ActionLabel.Text = "Ready"
    End Sub

    Private Sub FormatDoc()
        If Form1.CheckBox8.Checked Then
            Document.FormatDoc(TextBox7.Text & TextBox3.Text & ".t.xml", RichTextBox1)
        End If
    End Sub
End Class